<template>
  <div>
    <el-card shadow="never">
      <header-title title="商户信息"></header-title>
    </el-card>
    <el-card shadow="never">
      <el-row>
        <el-col :span="8">
          <div class="bdetails">
            <p>
              <b>商户名称：</b>{{defaultValue(businessDetailsData.businessName,'暂无信息')}}
            </p>
            <p>
              <b>摊位号：</b>{{defaultValue(businessDetailsData.stallName,'暂无信息')}}
            </p>
            <p>
              <b>性别：</b>{{defaultValue(businessDetailsData.sexName,'暂无信息')}}
            </p>
            <p>
              <b>身份证号码：</b>{{defaultValue(businessDetailsData.idNumber,'暂无信息')}}
            </p>
            <p>
              <b>手机号：</b>{{defaultValue(businessDetailsData.phone,'暂无信息')}}
            </p>
            <p style="margin: -9px 0;" v-show="businessDetailsData.businessFaceImg === ''">
              <b>商户头像：</b><el-button @click="showPictureDialog1 = true" :disabled="businessDetailsData.businessFaceImg === ''" type="text" style="color:#909399;">点击查看</el-button>
            </p>
            <p style="margin: -9px 0;" v-show="businessDetailsData.businessFaceImg !== ''">
              <b>商户头像：</b><el-button @click="showPictureDialog1 = true" :disabled="businessDetailsData.businessFaceImg === ''" type="text" style="color:#3dc2c2;">点击查看</el-button>
            </p>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="bdetails">
            <p>
              <b>银行账户：</b>{{defaultValue(businessDetailsData.account,'暂无信息')}}
            </p>
            <p>
              <b>开户行：</b>{{defaultValue(businessDetailsData.bank, '暂无信息')}}
            </p>
            <p>
              <b>开户名称：</b>{{defaultValue(businessDetailsData.accountName, '暂无信息')}}
            </p>
            <p>
              <b>食品经营许可证号码：</b>{{defaultValue(businessDetailsData.foodBusinessLicenseNumber, '暂无信息')}}
            </p>
            <p style="margin: -9px 0;" v-show="businessDetailsData.businessLicenseImg === ''">
              <b>企业营业执照：</b><el-button @click="showPictureDialog = true" :disabled="businessDetailsData.businessLicenseImg === ''" type="text" style="color:#909399;">点击查看</el-button>
            </p>
            <p style="margin: -9px 0;" v-show="businessDetailsData.businessLicenseImg !== ''">
              <b>企业营业执照：</b><el-button @click="showPictureDialog = true" :disabled="businessDetailsData.businessLicenseImg === ''" type="text" style="color:#3dc2c2;">点击查看</el-button>
            </p>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="bdetails">
            <p>
              <b>营业状态：</b>{{defaultValue(businessDetailsData.statusName,'暂无信息')}}
            </p>
            <p>
              <b>是否审核：</b>{{defaultValue(businessDetailsData.isAuditName, '暂无信息')}}
            </p>
            <p>
              <b>商户主营类型：</b>{{defaultValue(businessDetailsData.businessTypeName, '暂无信息')}}
            </p>
            <p>
              <b>法人名称：</b>{{defaultValue(businessDetailsData.legalPerson, '暂无信息')}}
            </p>
            <p>
              <b>负责人姓名：</b>{{defaultValue(businessDetailsData.personInCharge, '暂无信息')}}
            </p>
          </div>
        </el-col>
      </el-row>
    </el-card>
    <el-dialog
      title="企业营业执照图片"
      :visible.sync="showPictureDialog"
      width="70%"
      center>
      <div style="width: 100%;">
        <img :src="businessDetailsData.businessLicenseImg" style="width: 100%;" />
      </div>
    </el-dialog>
    <el-dialog
      title="商户头像图片"
      :visible.sync="showPictureDialog1"
      width="70%"
      center>
      <div style="width: 100%;">
        <img :src="businessDetailsData.businessFaceImg" style="width: 100%;" />
      </div>
    </el-dialog>
    <el-card shadow="never">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="交易信息" name="first">
          <el-table :data="transactionData" v-loading="loading" style="width: 100%">
            <el-table-column type="expand">
              <template slot-scope="props">
                <el-row>
                  <el-col :span="12">
                    <p>
                      <b>商户名称：{{defaultValue(props.row.business_name,'暂无信息')}}</b>
                    </p>
                    <p>
                      <b>商户地址：{{defaultValue(props.row.market_address,'暂无信息')}}</b>
                    </p>
                    <p>
                      <b>订单编号：{{defaultValue(props.row.code,'暂无信息')}}</b>
                    </p>
                    <p>
                      <b v-if="props.row.pay_way == 0">支付方式：现金</b>
                      <b v-if="props.row.pay_way == 1">支付方式：微信</b>
                      <b v-if="props.row.pay_way == 2">支付方式：支付宝</b>
                    </p>
                  </el-col>
                  <el-col :span="12">
                    <p>
                      <b>电子交易流水号：{{defaultValue(props.row.serial_number,'暂无信息')}}</b>
                    </p>
                    <p>
                      <b>市场名称：{{defaultValue(props.row.market_name, '暂无信息')}}</b>
                    </p>
                    <p>
                      <b>摊位号：{{defaultValue(props.row.stall_name, '暂无信息')}}</b>
                    </p>
                  </el-col>
                </el-row>
                <div>
                  <el-table :data="props.row.details" border style="width: 100%">
                    <el-table-column label="商品图片">
                      <template slot-scope="props">
                        <img :src="props.row.product_image" width="200" height="150"/>
                      </template>
                    </el-table-column>
                    <el-table-column prop="price" label="单价(元)"></el-table-column>
                    <el-table-column prop="weight" label="重量(kg)"></el-table-column>
                    <el-table-column prop="total" label="小计(元)"></el-table-column>
                  </el-table>
                </div>
                <p style="margin-bottom: 10px; font-size: 20px; float:right; font-weight: normal;">
                  总计：<span style="padding: 0 10px; color: red;">￥{{defaultValue(props.row.paid,'暂无信息')}}</span> <span style="font-size: 16px; text-decoration: line-through;">￥{{defaultValue(props.row.payable,'暂无信息')}}</span>
                </p>
              </template>
            </el-table-column>
            <el-table-column label="商户名称" prop="business_name"></el-table-column>
            <el-table-column label="订单编号" prop="code"></el-table-column>
            <el-table-column label="消费者" prop="membership_name"></el-table-column>
            <el-table-column label="消费金额(元)" prop="receivable"></el-table-column>
            <el-table-column label="购买时间" prop="start_time"></el-table-column>
            <el-table-column label="商品数量" prop="details_count"></el-table-column>
          </el-table>
          <el-pagination
            :page-size="transactionPage.perPage"
            :current-page="transactionPage.page"
            :total="transactionPage.total"
            @current-change="changeTransactionPage"
            style="margin-top: 15px;"
            background
            layout="prev, pager, next"
          ></el-pagination>
        </el-tab-pane>
        <el-tab-pane label="缴费记录" name="second">
          <el-table :data="payTheFeesData" v-loading="loading" style="width: 100%">
            <el-table-column prop="stallName" label="摊位名称"></el-table-column>
            <el-table-column prop="businessName" label="商户名称"></el-table-column>
            <el-table-column prop="payWayName" label="缴费方式"></el-table-column>
            <el-table-column prop="payTypeName" label="缴费项目"></el-table-column>
            <el-table-column prop="money" label="缴费金额(元)"></el-table-column>
            <el-table-column prop="content" label="缴费内容"></el-table-column>
            <el-table-column prop="marketAccountName" label="账户名称"></el-table-column>
            <el-table-column prop="payTheFeesTime" label="缴费时间"></el-table-column>
            <el-table-column prop="payStatusName" label="是否已缴费"></el-table-column>
          </el-table>
          <el-pagination
            :page-size="payTheFeesPage.perPage"
            :current-page="payTheFeesPage.page"
            :total="payTheFeesPage.total"
            @current-change="changePayTheFeesPage"
            style="margin-top: 15px;"
            background
            layout="prev, pager, next"
          ></el-pagination>
        </el-tab-pane>
        <el-tab-pane label="检测信息" name="third">
          <el-table :data="checkReportData" v-loading="loading" style="width: 100%">
            <el-table-column prop="stallName" label="摊位名称"></el-table-column>
            <el-table-column prop="businessName" label="商户名称"></el-table-column>
            <el-table-column prop="sampleName" label="样品名称/产品名称"></el-table-column>
            <el-table-column prop="sampleNumber" label="样品编号"></el-table-column>
            <el-table-column prop="detector" label="检测师"></el-table-column>
            <el-table-column prop="detectionMethod" label="检测方法"></el-table-column>
            <el-table-column prop="testingStandard" label="检测标准"></el-table-column>
            <el-table-column prop="testItem" label="检测项目"></el-table-column>
            <el-table-column prop="testResults" label="检测结果/检测值"></el-table-column>
            <el-table-column prop="detectionTime" label="检测时间"></el-table-column>
            <el-table-column prop="isQualifiedName" label="是否合格"></el-table-column>
          </el-table>
          <el-pagination
            :page-size="checkReportPage.perPage"
            :current-page="checkReportPage.page"
            :total="checkReportPage.total"
            @current-change="changeCheckReportPage"
            style="margin-top: 15px;"
            background
            layout="prev, pager, next"
          ></el-pagination>
        </el-tab-pane>
        <el-tab-pane label="填报信息" name="fourth">
          <el-table :data="purchaseDetailData" v-loading="loading" style="width: 100%">
            <el-table-column prop="businessName" label="商户名称"></el-table-column>
            <el-table-column prop="stallName" label="摊位号"></el-table-column>
            <el-table-column prop="businessTypeStr" label="经营类别"></el-table-column>
            <el-table-column prop="purchaseTime" label="填报日期"></el-table-column>
            <el-table-column prop="number" label="商品数量"></el-table-column>
            <el-table-column prop="supplierName" label="供应商"></el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="合同" name="five">
          <el-table :data="contractData" v-loading="loading" style="width: 100%">
            <el-table-column prop="signDate" label="签订日期"></el-table-column>
            <el-table-column prop="months" label="时长(月)"></el-table-column>
            <el-table-column prop="dueTime" label="到期时间"></el-table-column>
            <el-table-column prop="businessName" label="商户名称"></el-table-column>
            <el-table-column prop="stallName" label="摊位名称"></el-table-column>
            <el-table-column prop="contractAmount" label="合同金额(元)"></el-table-column>
            <el-table-column prop="contractNumber" label="合同编号"></el-table-column>
            <el-table-column prop="contractStatus" label="合同状态"></el-table-column>
          </el-table>
          <el-pagination
            :page-size="contractPage.perPage"
            :current-page="contractPage.page"
            :total="contractPage.total"
            @current-change="changeContractPage"
            style="margin-top: 15px;"
            background
            layout="prev, pager, next"
          ></el-pagination>
        </el-tab-pane>
        <el-tab-pane label="健康证" name="six">
          <el-button @click="addHealth" class="button" size="small" round>添加健康证</el-button>
          <el-table :data="healthData" v-loading="loading" style="width: 100%">
            <el-table-column prop="businessName" label="商户名称"></el-table-column>
            <el-table-column prop="healthName" label="健康证持有人"></el-table-column>
            <el-table-column prop="healthPhoto" label="健康证图片">
              <template slot-scope="scope">
                <img :src="scope.row.healthPhoto" alt="" style="height: 3rem;">
              </template>
            </el-table-column>
            <el-table-column prop="healthDate" label="健康证有限期"></el-table-column>
            <el-table-column label="操作" width="125">
              <template slot-scope="scope">
                <el-button @click="healthDelete(scope.row)" type="text" style="color:#dc1033;">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :page-size="healthPage.perPage"
            :current-page="healthPage.page"
            :total="healthPage.total"
            @current-change="changeHealthPage"
            style="margin-top: 15px;"
            background
            layout="prev, pager, next"
          ></el-pagination>
        </el-tab-pane>
        <el-tab-pane label="许可证" name="seven">
          <el-button @click="addLicense" class="button" size="small" round>添加许可证</el-button>
          <el-table :data="licenseData" v-loading="loading" style="width: 100%">
            <el-table-column prop="otherBusinessLicenseName" label="许口证持有人"></el-table-column>
            <el-table-column prop="otherBusinessLicenseNumber" label="许口证编号"></el-table-column>
            <el-table-column prop="typeName" label="许可证类型"></el-table-column>
            <el-table-column prop="otherBusinessLicenseExpirationDate" label="许可证有效期"></el-table-column>
            <el-table-column prop="otherBusinessLicenseImg" label="许可证照片">
              <template slot-scope="scope">
                <img :src="scope.row.otherBusinessLicenseImg" alt="" style="height: 3rem;">
              </template>
            </el-table-column>
            <el-table-column label="操作" width="125">
              <template slot-scope="scope">
                <el-button @click="licenseDelete(scope.row)" type="text" style="color:#dc1033;">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :page-size="licensePage.perPage"
            :current-page="licensePage.page"
            :total="licensePage.total"
            @current-change="changeLicensePage"
            style="margin-top: 15px;"
            background
            layout="prev, pager, next"
          ></el-pagination>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <el-dialog title="添加健康证" :visible.sync="healthDialog" @close="close">
      <div>
        <el-form :model="model" label-width="150px">
          <!--<el-form-item label="商户名称：" prop="businessName">
            <el-input v-model="model.businessName"></el-input>
          </el-form-item>-->
          <el-form-item label="健康证持有人：" prop="healthName">
            <el-input v-model="model.healthName"></el-input>
          </el-form-item>
          <el-form-item label="健康证有效期：" prop="healthDate">
            <el-input v-model="model.healthDate"></el-input>
          </el-form-item>
          <el-form-item label="健康证照片：" prop="healthPhoto">
            <!--<el-input v-model="model.healthPhoto"></el-input>-->
            <el-upload class="avatar-uploader" ref="healthPhoto" style="display: inline-block;"
                       action="https://upload.qiniup.com"
                       :on-remove='handleRemove'
                       :on-success="handleSuccesshealthPhoto"
                       :show-file-list="false"
                       :limit='1'
                       :data='uploadToken'
                       :before-upload="beforeUpload"
            >
              <img v-if="model.healthPhoto" :src="model.healthPhoto" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save">保存健康证</el-button>
      </div>
    </el-dialog>
    <el-dialog title="添加许可证" :visible.sync="licenseDialog" @close="close">
      <div>
        <el-form :model="modelLicense" label-width="150px">
          <el-form-item label="许口证持有人：" prop="otherBusinessLicenseName">
            <el-input v-model="modelLicense.otherBusinessLicenseName"></el-input>
          </el-form-item>
          <el-form-item label="许口证编号：" prop="otherBusinessLicenseNumber">
            <el-input v-model="modelLicense.otherBusinessLicenseNumber"></el-input>
          </el-form-item>
          <el-form-item label="许可证类型：" prop="type">
            <el-select v-model="modelLicense.type" @change="changeType" placeholder="请选择">
              <el-option :key="0" label="食品经营许可证" :value="0"></el-option>
              <el-option :key="1" label="小作坊经营许可证" :value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="许可证有效期：" prop="otherBusinessLicenseExpirationDate">
            <el-input v-model="modelLicense.otherBusinessLicenseExpirationDate"></el-input>
          </el-form-item>
          <el-form-item label="许可证照片：" prop="otherBusinessLicenseImg">
            <el-upload class="avatar-uploader" ref="otherBusinessLicenseImg" style="display: inline-block;"
                       action="https://upload.qiniup.com"
                       :on-remove='handleRemove'
                       :on-success="handleSuccessotherBusinessLicenseImg"
                       :show-file-list="false"
                       :limit='1'
                       :data='uploadToken'
                       :before-upload="beforeUpload"
            >
              <img v-if="modelLicense.otherBusinessLicenseImg" :src="modelLicense.otherBusinessLicenseImg" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveLicense">保存许可证</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import HeaderTitle from "@/components/HeaderTitle"
import { isNil, formatDate } from "@/utils"
import Cookies from 'js-cookie'

export default {
  name: 'BusinessList',
  components: {
     HeaderTitle
  },
  data(){
    return {
      marketId: 0,
      loading: true,
      activeName: 'first',
      businessId: 0,
      code: '',
      startDate: '',
      endDate: '',
      payWay: '',

      // 商户详情
      businessDetailsData: {},
      businessLicenseImg: '',
      showPictureDialog: false,
      showPictureDialog1: false,

      // 合同
      contractData: [],
      status: 0, // search
      contractPage: {
        perPage: 10, // 每页显示10条
        page: 1, // 当前页
        total: 0
      },

      // 缴费记录
      payTheFeesData: [],
      searchPayTheFees: {  // search
        businessName: '',
        payStatus: -1,  // 是否已缴费 -1全部 0已缴 1未缴
        payType: -1  // 缴费类型 -1全部 1水电 2 房租 3 垃圾费
      },
      payTheFeesPage: {
        perPage: 10, // 每页显示10条
        page: 1, // 当前页
        total: 0
      },

      // 检测信息
      checkReportData: [],
      searchCheckReport: {
        businessName: ''
      },
      checkReportPage: {
        perPage: 10, // 每页显示10条
        page: 1, // 当前页
        total: 0
      },

      // 填报信息
      purchaseDetailData:[],

      // 交易信息
      transactionData: [],
      transactionPage: {
        perPage: 10, // 每页显示10条
        page: 1, // 当前页
        total: 0
      },

      // 健康证
      healthData: [],
      healthPage: {
        perPage: 10, // 每页显示10条
        page: 1, // 当前页
        total: 0
      },
      healthDialog: false,
      model: {
        businessId: 0, //	int	true	商户ID
        businessName: '', //	string	true	商户名称
        healthName: '', //	string	true	健康证持有人
        healthPhoto: '', //	string	true	健康证照片
        healthDate: '' //	string	true	健康证有效期
      },
      uploadToken:{
        key: '',
        token: ''
      },
      tabName: 0,

      // 许可证
      licenseData: [],
      licensePage: {
        perPage: 10, // 每页显示10条
        page: 1, // 当前页
        total: 0
      },
      licenseDialog: false,
      modelLicense: {
        businessId: 0, //	int	true	商户ID
        otherBusinessLicenseName: '', //	string	true	许口证持有人
        otherBusinessLicenseNumber: '', //	string	true	许口证编号
        otherBusinessLicenseExpirationDate: '', //	string	true	许可证有效期
        otherBusinessLicenseImg: '', //	string	true	许可证照片
        type: 0,
      },
    }
  },
  watch: {
    '$store.state.app.marketId'(val, oldVal){
      const query = this.$route.query
      this.businessId = query.businessId / 1
      this.model.businessId = query.businessId / 1
      this.modelLicense.businessId = query.businessId / 1
      this.marketId = val
      this.goodsToken()

      this.getTransactionList(val, this.businessId, this.code, this.startDate, this.endDate, this.payWay, this.transactionPage.perPage, this.transactionPage.page)
      this.getBusinessDetails(this.businessId)
    }
  },
  mounted(){
    // const marketData = JSON.parse(Cookies.get('MarketData'))
    // this.marketName = marketData[0].name
    this.marketId = this.$store.state.app.marketId

    const query = this.$route.query
    this.businessId = query.businessId / 1
    this.model.businessId = query.businessId / 1
    this.modelLicense.businessId = query.businessId / 1

    this.getTransactionList(this.marketId, this.businessId, this.code, this.startDate, this.endDate, this.payWay, this.transactionPage.perPage, this.transactionPage.page)
    this.getBusinessDetails(this.businessId)

    this.goodsToken()

    if(this.tabName === 6){
      this.getHealthList(this.businessId, this.healthPage.perPage, this.healthPage.page)
    }
    if(this.tabName === 6){
      this.getLicenseList(this.businessId, this.healthPage.perPage, this.healthPage.page)
    }
  },
  methods:{
    // 新增许可证类型
    changeType(val){
      this.modelLicense.type = val
    },
    defaultValue (value, defaultValue) {
      if (isNil(value)) {
        return defaultValue
      }
      return value
    },
    handleClick(tab, event) {
      // console.log(tab, event)
      this.loading = false
      if(this.tabName === 6){
        tab.name === 'six'
      }
      if(this.tabName === 7){
        tab.name === 'seven'
      }
      if(tab.name === 'first'){
        this.transactionPage.page = 1
        this.getTransactionList(this.marketId, this.businessId, this.code, this.startDate, this.endDate, this.payWay, this.transactionPage.perPage, this.transactionPage.page)
      }else if(tab.name === 'second'){
        this.payTheFeesPage.page = 1
        this.getPayTheFeesList(this.businessId, this.searchPayTheFees.businessName, this.searchPayTheFees.payStatus, this.searchPayTheFees.payType, this.payTheFeesPage.perPage, this.payTheFeesPage.page)
      }else if(tab.name === 'third'){
        this.checkReportPage.page = 1
        this.getCheckReportList(this.businessId, this.searchCheckReport.businessName, this.payTheFeesPage.perPage, this.payTheFeesPage.page)
      }else if(tab.name === 'fourth'){
        this.getPurchaseDetail(this.businessId)
      }else if(tab.name === 'five'){
        this.contractPage.page = 1
        this.getContractList(this.businessId, this.status, this.contractPage.perPage, this.contractPage.page)
      }else if(tab.name === 'six'){
        this.getBusinessDetails(this.businessId)
        this.getHealthList(this.businessId, this.healthPage.perPage, this.healthPage.page)
      }else if(tab.name === 'seven'){
        this.getLicenseList(this.businessId, this.licensePage.perPage, this.licensePage.page)
      }
    },
    // 获取商户详情
    getBusinessDetails(businessId){
      this.$store.dispatch('business/fetchBusinessDetails',{businessId}).then(
        data => {
          this.loading = false
          if(data.data === {}){
            this.businessDetailsData = {}
          }else{
            this.businessDetailsData = data.data
          }
        }
      ).catch(err => {
        this.loading = false
        this.businessDetailsData = {}
      })
    },
    // 合同列表
    getContractList(businessId,status,perPage,page){
      this.$store.dispatch('business/fetchContractList',{businessId,status,perPage,page}).then(
        data => {
          this.loading = false
          if(data.data.items.length === 0){
            this.contractData = []
            this.contractPage.total = 0
          }else{
            for(let i = 0; i < data.data.items.length; i++){
              data.data.items[i].contractAmount = data.data.items[i].contractAmount / 100
              if(data.data.items[i].contractStatus === 1){
                data.data.items[i].contractStatus = '正常'
              }else if(data.data.items[i].contractStatus === 2){
                data.data.items[i].contractStatus = '合同作废'
              }
              else if(data.data.items[i].contractStatus === 3){
                data.data.items[i].contractStatus = '合同解除'
              }else if(data.data.items[i].contractStatus === 4){
                data.data.items[i].contractStatus = '合同续签'
              }
            }
            this.contractData = data.data.items
            this.contractPage.total = data.data.total
          }
        }
      ).catch(err => {
        this.loading = false
        this.contractData = []
        this.contractPage.total = 0
      })
    },
    changeContractPage(num){
      this.contractPage.page = num
      this.getContractList(this.businessId, this.status, this.contractPage.perPage, num)
    },

    // 缴费记录(缴费列表)
    getPayTheFeesList(businessId,businessName,payStatus,payType,perPage,page){
      this.$store.dispatch('business/fetchPayTheFeesList',{businessId,businessName,payStatus,payType,perPage,page}).then(
        data => {
          this.loading = false
          for(let i = 0; i < data.data.items.length; i++){
            data.data.items[i].money = data.data.items[i].money / 100
          }
          this.payTheFeesData = data.data.items
          this.payTheFeesPage.total = data.data.total
        }
      ).catch(err => {
        this.loading = false
        this.payTheFeesData = []
        this.payTheFeesPage.total = 0
      })
    },
    changePayTheFeesPage(num){
      this.payTheFeesPage.page = num
      this.getPayTheFeesList(this.businessId, this.searchPayTheFees.businessName, this.searchPayTheFees.payStatus, this.searchPayTheFees.payType, this.payTheFeesPage.perPage, num)
    },

    // 检测信息列表
    getCheckReportList(businessId,businessName,perPage,page){
      this.$store.dispatch('business/fetchCheckReportList',{businessId,businessName,perPage,page}).then(
        data => {
          this.loading = false
          this.checkReportData = data.data.items
          this.checkReportPage.total = data.data.total
        }
      ).catch(err => {
        this.loading = false
        this.checkReportData = []
        this.checkReportPage.total = 0
      })
    },
    changeCheckReportPage(num){
      this.checkReportPage.page = num
      this.getCheckReportList(this.businessId, this.searchCheckReport.businessName, this.checkReportPage.perPage, num)
    },

    // 购进信息详情
    getPurchaseDetail(businessId){
      this.$store.dispatch('business/fetchPurchaseDetail',{businessId}).then(
        data => {
          this.loading = false
          if(data.data.items.length === 0){
            this.purchaseDetailData = []
          }else{
            this.purchaseDetailData = data.data.items
          }
        }
      ).catch(err => {
        this.loading = false
        this.purchaseDetailData = []
      })
    },

    // 商户交易信息
    getTransactionList(marketId, businessId, code, startDate, endDate, payWay, perPage, page){
      this.$store.dispatch('business/fetchTransactionList',{marketId, businessId, code, startDate, endDate, payWay, perPage, page}).then(
        data => {
          this.loading = false
          for (let i = 0; i < data.Data.Items.length; i++) {
            data.Data.Items[i].receivable = (data.Data.Items[i].receivable === '-' ? data.Data.Items[i].receivable : (data.Data.Items[i].receivable / 100))
            data.Data.Items[i].paid = (data.Data.Items[i].paid / 100).toFixed(2)
            data.Data.Items[i].payable = (data.Data.Items[i].payable / 100).toFixed(2)
            for (let j = 0; j < data.Data.Items[i].details.length; j++) {
              data.Data.Items[i].details[j].price = data.Data.Items[i].details[j].price / 100
              data.Data.Items[i].details[j].total = data.Data.Items[i].details[j].total / 100
              data.Data.Items[i].details[j].weight = data.Data.Items[i].details[j].weight / 1000
            }
          }

          this.transactionData = data.Data.Items
          this.transactionPage.total = data.Data.total
        }
      ).catch(err => {
        this.loading = false
        this.transactionData = []
        this.transactionPage.total = 0
      })
    },
    changeTransactionPage(num){
      this.transactionPage.page = num
      this.getTransactionList(this.marketId, this.businessId, this.code, this.startDate, this.endDate, this.payWay, this.transactionPage.perPage, num)
    },

    // 健康证列表
    getHealthList(businessId,perPage,page){
      this.$store.dispatch('business/fetchHealthList',{businessId,perPage,page}).then(
        data => {
          this.loading = false
          this.healthData = data.items
          this.healthPage.total = data.total
        }
      ).catch(err => {
        this.loading = false
        this.healthData = []
        this.healthPage.total = 0
      })
    },
    changeHealthPage(num){
      this.healthPage.page = num
      this.getHealthList(this.businessId, this.healthPage.perPage, num)
    },
    // 点击 新增 健康证 按钮
    addHealth(){
      this.healthDialog = true
    },
    handleSuccesshealthPhoto(res, file, fileList){
      this.model.healthPhoto = 'http://cailanzi.image.cailanzi001.com/' + res.key
      this.$refs.healthPhoto.clearFiles()
    },
    handleRemove(file, fileList) {
      console.log(file,fileList[0])
    },
    beforeUpload(file){
      const isPNG = file.type === "image/png"
      const isJPEG = file.type === "image/jpeg"
      const isJPG = file.type === "image/jpg"
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isPNG && !isJPEG && !isJPG) {
        this.$message.error("上传头像图片只能是 jpg、png、jpeg 格式!")
        return false
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!")
        return false
      }
      //将上传的文件名保存成key,把这个key拼接到外链地址
      const date = new Date()
      this.uploadToken.key=`bill${date.getFullYear()}${date.getMonth()+1}${date.getDate()}${date.getHours()}${date.getMinutes()}${date.getSeconds()}${Math.floor(Math.random()*100)}.${file.type.split('/')[1]}`
    },
    //获取上传文件时候的token
    goodsToken(){
      this.$store.dispatch('purchaseInfo/uptoken',{}).then(
        data => {
          this.uploadToken.token = data.data.uptoken
        }
      ).catch(err => {
      })
    },

    // 新增健康证
    save(){
      const query = this.$route.query
      this.businessId = query.businessId / 1
      this.model.businessId = query.businessId / 1
      this.$store.dispatch("business/fetchHealthAddSave", this.model).then(data => {
        this.tabName = 6
        this.healthDialog = false
        if(data){
          this.$message({
            type: 'success',
            message: '新增成功，请刷新！'
          })
        }
        this.getHealthList(this.businessId, this.healthPage.perPage, this.healthPage.page)
        this.clear()
        this.$refs.healthPhoto.clearFiles()
      })
    },
    // 获取详情健康证
    fetch(healthId){
      this.$store.dispatch("business/fetchHealthDetails", {healthId}).then(data=>{
        this.model = data
      })
    },
    close(){
      this.clear()
    },
    clear(){
      this.model = {
        businessId: 0, //	int	true	商户ID
        businessName: '', //	string	true	商户名称
        healthName: '', //	string	true	健康证持有人
        healthPhoto: '', //	string	true	健康证照片
        healthDate: '' //	string	true	健康证有效期
      }
      this.modelLicense = {
        businessId: 0, //	int	true	商户ID
        otherBusinessLicenseName: '', //	string	true	许口证持有人
        otherBusinessLicenseNumber: '', //	string	true	许口证编号
        otherBusinessLicenseExpirationDate: '', //	string	true	许可证有效期
        otherBusinessLicenseImg: '', //	string	true	许可证照片
        type: 0,
      }
    },
    healthDelete(row){
      const healthId = row.healthId
      this.$confirm(`是否确定要删除商户 "${row.healthName}"`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch("business/fetchHealthDelete", {healthId}).then(data=>{
          if(data){
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
          }
          this.clear()
          this.getHealthList(this.businessId, this.healthPage.perPage, this.healthPage.page)
        })
      })
    },


    // 许可证列表
    getLicenseList(businessId,perPage,page){
      this.$store.dispatch('business/fetchLicenseList',{businessId,perPage,page}).then(
        data => {
          this.loading = false
          this.licenseData = data.items
          this.licensePage.total = data.total
        }
      ).catch(err => {
        this.loading = false
        this.licenseData = []
        this.licensePage.total = 0
      })
    },
    changeLicensePage(num){
      this.licensePage.page = num
      this.getLicenseList(this.businessId, this.licensePage.perPage, num)
    },
    // 点击 新增 许可证 按钮
    addLicense(){
      this.licenseDialog = true
    },
    handleSuccessotherBusinessLicenseImg(res, file, fileList){
      this.modelLicense.otherBusinessLicenseImg = 'http://cailanzi.image.cailanzi001.com/' + res.key
      this.$refs.otherBusinessLicenseImg.clearFiles()
    },

    // 新增许可证
    saveLicense(){
      const query = this.$route.query
      this.businessId = query.businessId / 1
      this.modelLicense.businessId = query.businessId / 1
      this.$store.dispatch("business/fetchLicenseAddSave", this.modelLicense).then(data => {
        this.tabName = 7
        this.licenseDialog = false
        if(data){
          this.$message({
            type: 'success',
            message: '新增成功，请刷新！'
          })
        }
        this.getLicenseList(this.businessId, this.licensePage.perPage, this.licensePage.page)
        this.clear()
        this.$refs.otherBusinessLicenseImg.clearFiles()
      })
    },
    // 获取详情许可证
    fetch(otherBusinessLicenseId){
      this.$store.dispatch("business/fetchLicenseDetails", {otherBusinessLicenseId}).then(data=>{
        this.modelLicense = data
      })
    },
    licenseDelete(row){
      const otherBusinessLicenseId = row.otherBusinessLicenseId
      this.$confirm(`是否确定要删除许可证 "${row.otherBusinessLicenseName}"`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch("business/fetchLicenseDelete", {otherBusinessLicenseId}).then(data=>{
          if(data){
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
          }
          this.clear()
          this.getLicenseList(this.businessId, this.licensePage.perPage, this.licensePage.page)
        })
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.button {
  background: #64d9d6;
  color: #fff;
  margin-left: 40px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #d9d9d9;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
